import DataBus from './databus'
import DItem from './d_item'

let databus = new DataBus()
let start_x = databus.start_x
let start_y = databus.start_y
let c_height = databus.c_height
let line_num = databus.line_num
let diamondSize = databus.diamondSize

//方块图片集合 - 种类
const RECT_IMGS = ['images/rect_1.png', 'images/rect_2.png', 'images/rect_3.png', 'images/rect_4.png', 'images/rect_5.png', 'images/rect_6.png', 'images/rect_7.png', 'images/rect_8.png',]

export default class DItems {
  constructor() {
    this.items = []
    this.offect_x = 0
    // 状态：0:初始化 1:运动状态2:将要停止状态3:停止状态4:结算状态
    this.state = 0

  }

  randomItems() {

    let list_d1 = [
      [9, 0, 1, 1, 5],
      [10, 0, 1, 1, 5],
      [9, 1, 1, 1, 5],
      [10, 1, 1, 1, 5]
    ];
    let list_d2 = [
      [9, 0, 1, 1, 1],
      [9, 1, 1, 1, 4],
      [9, 1 * 2, 1, 1, 7],
      [10, 1 * 2, 1, 1, 8]
    ];
    let list_d3 = [

      [10, 0, 1, 1, 2],

      [10, 1, 1, 1, 5],
      [9, 1 * 2, 1, 1, 7],
      [10, 1 * 2, 1, 1, 8]
    ];
    let list_d4 = [

      [10, 0, 1, 1, 2],
      [9, 1, 1, 1, 4],
      [10, 1, 1, 1, 5],

      [10, 1 * 2, 1, 1, 8]
    ];
    let list_d5 = [

      [10, 0, 1, 1, 2],
      [9, 1, 1, 1, 4],
      [10, 1, 1, 1, 5],
      [9, 1 * 2, 1, 1, 7],

    ];
    let list_d6 = [
      [9, 0, 1, 1, 1],

      [9, 1, 1, 1, 4],
      [10, 1, 1, 1, 5],

      [10, 1 * 2, 1, 1, 8]
    ];
    let list_d7 = [
      [10, 0, 1, 1, 2],
      [10, 1, 1, 1, 5],
      [10, 1 * 2, 1, 1, 8]
    ];
    let for_randoms = [list_d1, list_d2, list_d3, list_d4, list_d5, list_d6, list_d7];
    let list_d_one = for_randoms[Math.floor(Math.random() * for_randoms.length)];
    let img_one_str = RECT_IMGS[Math.floor(Math.random() * RECT_IMGS.length)];
    let items = []
    for (let i = 0; i < list_d_one.length; i++){
      let item_info = list_d_one[i];
      let item = databus.pool.getDItemByClass('DItem', DItem,img_one_str, item_info[0], item_info[1], item_info[2], item_info[3], item_info[4])
      item.reInit(img_one_str, item_info[0], item_info[1], item_info[2], item_info[3], item_info[4]);
      items[i] = item
    }

    return items;
  }

  // 获取方块
  init() {
    this.items = this.randomItems();

    this.state = 0;
  }

  update(){

    let do_will = true //假定可以
    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      do_will = item.willDown(0)
      if (do_will === false) {
        break;
      }
    }
    if (do_will === false){//有不能下落的则无法下落

      this.handleItems()
    } else {//全部可以下落，执行更新下落
      for (let i = 0; i < this.items.length; i++) {
        let item = this.items[i];
        item.didDown()
      }
    }
  }

  handleItems(){
    databus.receiveDiamond(this.items);
    databus.run_diamonds.splice(0, 1);
  }

  drawToCanvas(ctx) {
    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      item.drawToCanvas(ctx)
    }
  }

  turn(isLeft) {
    if (isLeft) {

      this.offect_x -= 1;
      this.didTurn()
    } else if (!isLeft) {

      this.offect_x += 1;
      this.didTurn()
    }
  }

  didTurn(){
    let do_will = true //假定可以
    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      do_will = item.willTurn(this.offect_x)
      if (do_will === false) {
        break;
      }
    }

    if (do_will === false) {//存在0,无法移动 - 更改为直接平移，2020.3.7，livesxu

    // } else if (turn.includes(2)) {//存在2,下移
    //   for (let i = 0; i < this.items.length; i++) {
    //     let item = this.items[i];
    //     item.didTurn(this.offect_x, 2)
    //   }
    } else {//全是1，平移
      for (let i = 0; i < this.items.length; i++) {
        let item = this.items[i];
        item.didTurn(this.offect_x, 1)
      }
    }
    this.offect_x = 0;
  }

  transform() {

    let do_will = true //假定可以
    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      do_will = item.willTransform()
      if (do_will === false) {
        break;
      }
    }
    if (do_will === false) {

    } else {//全部可以变换，更新变换
      for (let i = 0; i < this.items.length; i++) {
        let item = this.items[i];
        item.didTransform();
      }
    }
  }
}